package globulin;

import globulin.soundFXProccessing.SoundFX;
import jgame.*;
import jgame.platform.JGEngine.*;
import jgame.impl.JGEngineInterface.*;

/** This class defines the guns on the boss.
 * @author Sean Pavlichek
 */
public class BossNode extends JGObject {
    // Stores the boss to use certain methods

    private Boss parent;
    // Defines graphic names
    private String[] bossNodes = new String[]{"bossnode1", "bossnode2", "bossnode3"};
    private String[] bossNodesHit = new String[]{"bossnode1h", "bossnode2h", "bossnode3h"};
    private String[] fireImages = new String[]{"boss_fire1", "boss_fire2", "boss_fire3"};
    // Other necessary variables
    private boolean firing = false;
    private boolean ceasingFire = false;
    private int timer;
    private int life = 15;

    /** Basic constructor.
     * @param inputParent The boss that this gun is attached to.
     * @param inputX The x position of this gun.
     * @param inputY The y position of this gun.
     */
    public BossNode(Boss inputParent, double inputX, double inputY) {
        // String name, boolean unique_id, double x, double y, int collisionID,
        // String gfxName, double xSpeed, double ySpeed
        super("boss", true, inputX, inputY, Globulin.ENEMY_COLID, "bossnode1", inputParent.xspeed, 0);

        parent = inputParent;
    }

    /** Overrrides the default move command.
     */
    public void move() {
        // If this gun was previously hit and the timer has gone 20 ticks, resets the image
        if (this.timer % 20 == 0 && this.getGraphic().equals(bossNodesHit[2])) {
            this.setGraphic(bossNodes[2]);
        }

        // If the gun is in firing mode
        if (firing) {
            // Increments the timer
            timer += 1;

            // Opens the turret
            if ((timer % 30 == 0) && this.getGraphic().equals(bossNodes[0])) {
                this.setGraphic(bossNodes[1]);
            } else if ((timer % 30 == 0) && this.getGraphic().equals(bossNodes[1])) {
                this.setGraphic(bossNodes[2]);
            }

            // If the turret is fully open, fires
            if (this.getGraphic().equals(bossNodes[2]) && timer % 2 == 0) {
                new JGObject("en_missle", true, (x + (this.tilewidth / 2)),
                        (y + (this.tileheight / 2)), Globulin.ENEMY_MISSILE_COLID,
                        fireImages[eng.random(0, 2, 1)], 0, 4);
            }
        }

        // If the turret is being closed
        if (ceasingFire) {
            // Increments the timer
            timer += 1;

            // Closes the turret
            if ((timer % 30 == 0) && (this.getGraphic().equals(bossNodes[2]) || this.getGraphic().equals(bossNodesHit[2]))) {
                this.setGraphic(bossNodes[1]);
            } else if ((timer % 30 == 0) && (this.getGraphic().equals(bossNodes[1]) || this.getGraphic().equals(bossNodesHit[1]))) {
                this.setGraphic(bossNodes[0]);
            } else if (this.getGraphic().equals(bossNodes[0]) || this.getGraphic().equals(bossNodesHit[0])) {
                ceasingFire = false;
            }
        }

        // Ensures that the speed of this turret always matches the boss's
        this.xspeed = parent.xspeed;
    }

    /** Overrides the default hit command.
     * @param other The object that hit this turret.
     */
    public void hit(JGObject other) {
        // If what hit this turret was the player's laser
        if (other.colid == Globulin.PLAYER_LASER_COLID && this.getGraphic().equals(bossNodes[2])) {
            // Lowers the remaining life of the turret
            this.life -= 1;

            // Changes the graphic for a short amount of time
            this.setGraphic(bossNodesHit[2]);

            // Removes the laser
            other.remove();

            // If this turret's life is now 0 or below
            if (this.life <= 0) {
                // Adds score
                parent.gameScreen.score += 100;
                parent.gameScreen.checkScoreForNewLife();

                // Show an explosion
                new JGObject("explosion", true, x, y, 0, "explosion", 0, 0, 32);

                // Play a sound effect
                parent.gameScreen.soundFxThreadPool.execute(new SoundFX(parent.gameScreen, "boomfx"));

                // Removes this turret
                this.remove();
            }
        }
    }

    /** Turns the turret on.
     */
    public void openFire() {
        this.firing = true;
        this.ceasingFire = false;
        this.timer = 0;
    }

    /** Turns the turret off.
     */
    public void ceaseFire() {
        this.firing = false;
        this.ceasingFire = true;
        this.timer = 0;
    }
}
